package com.aftvc.top.utils;

import com.aftvc.top.dao.UserDao;
import com.aftvc.top.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.function.Consumer;

/*
 *
 * mapper代理方式
 *
 * */
@Slf4j(topic = "ResourcesDemo2")
public class ResourcesDemo2 {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        log.info("------查询所有------");
        //查询所有
        mapper.selectUser()
                .forEach(System.out::println);


        log.info("-----查询单个通过id-----");
        //查询单个 通过id
        log.info(mapper.selectUserById(1).toString());

        log.info("-----查询单个通过username-----");
        //查询单个 通过username
        log.info(mapper.selectUserByName("严航").toString());


    }
}
